﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace syc.Juece.Report
{
    public partial class 分厂月主要产品产量表 : System.Web.UI.Page
    {
        protected double[] 计划量 = new double[12];//计划量
        protected double[] 实际量 = new double[12];//实际量
        protected double[] 完成率 = new double[12];//完成率
        protected string 品名;
        protected string 钢级;
        protected string 单位;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
                this.Year0.SelectedValue = DateTime.Today.Year.ToString();
                if (this.PinMing.SelectedItem.Text == "油管")
                {
                    this.SqlDataSource3.SelectCommand = "SELECT [ID], [名称] FROM [生产单位]";
                    this.SqlDataSource3.FilterExpression = " 名称='二分厂' or 名称='五分厂'";
                    this.FenChang.DataBind();
                }
                
            }
        }

        protected double  月计划量(int month,string 分厂,string 品名)
        {
            double 计划量=0;
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            if (分厂 == "一分厂" && 品名 == "套管")
            {
                string sql = @"select 一分厂产品2 from 生产计划 where 月份=" + month + "and 年度=" + this.Year0.SelectedValue;
                Cmd.CommandText = sql;
            }
            if (分厂 == "二分厂" && 品名 == "油管")
            {
                string sql = @"select 二分厂产品1 from 生产计划 where 月份=" + month + "and 年度=" + this.Year0.SelectedValue;
                Cmd.CommandText = sql;
            }
            if (分厂 == "二分厂" && 品名 == "套管")
            {
                string sql = @"select 二分厂产品2 from 生产计划 where 月份=" + month + "and 年度=" + this.Year0.SelectedValue;
                Cmd.CommandText = sql;
            }
            if (分厂 == "三分厂" && 品名 == "油管接箍")
            {
                string sql = @"select (三分厂产品1) from 生产计划 where 月份=" + month + "and 年度=" + this.Year0.SelectedValue;
                Cmd.CommandText = sql;
            }
            if (分厂 == "三分厂" && 品名 == "套管接箍")
            {
                string sql = @"select (三分厂产品2) from 生产计划 where 月份=" + month + "and 年度=" + this.Year0.SelectedValue;
                Cmd.CommandText = sql;
            }
            if(分厂 == "四分厂" && 品名 == "螺纹保护器")
            {
                string sql = @"select 四分厂产品2 from 生产计划 where 月份=" + month + "and 年度=" + this.Year0.SelectedValue;
                Cmd.CommandText = sql;
            }
            if (分厂 == "五分厂" && 品名 == "油管")
            {
                string sql = @"select 五分厂产品1 from 生产计划 where 月份=" + month + "and 年度=" + this.Year0.SelectedValue;
                Cmd.CommandText = sql;
            }         
           SqlDataReader Reader = Cmd.ExecuteReader();
           if (Reader.Read())
           {
               if (Reader.IsDBNull(0))
               {
                   计划量 = 0;
               }
               else
               {
                   计划量 = Reader.GetDouble(0);
               }
           }
           Reader.Close();
           Conn.Close();
           Reader.Dispose();
           Cmd.Dispose();
           Conn.Dispose();
            return 计划量;
        }
        //月实际量
        protected double 月实际量(int month, string 分厂, string 品名)
        {
            double 实际量 = 0;
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            if (分厂 == "一分厂" && 品名 == "套管")
            {
                string sql = @"
                 declare @yc smalldatetime
                 declare @ym smalldatetime
                 select @yc=起始日期,@ym=截止日期 from 生产月设置
                 where 分厂=1 and 年度='" + this.Year0.SelectedValue + "' and 月份='" + month + "' select isnull(sum(入库重量),0)/1000 from 生产进度 where 分厂ID=1 and 产品类别ID=2 and 日期 between @yc and @ym ";
                
                Cmd.CommandText = sql;
            }
            if (分厂 == "二分厂" && 品名 == "油管")
            {
                string sql = @"
                declare @yc smalldatetime
                declare @ym smalldatetime
                select @yc=起始日期,@ym=截止日期 from 生产月设置
                where 分厂=2 and 年度='" + this.Year0.SelectedValue + "' and 月份='" + month + "' select isnull(sum(入库重量),0)/1000 from 生产进度 where 分厂ID=2 and 产品类别ID=1 and 日期 between @yc and @ym";
               
                Cmd.CommandText = sql;
            }
            if (分厂 == "二分厂" && 品名 == "套管")
            {
                string sql = @"
                declare @yc smalldatetime
                declare @ym smalldatetime
                select @yc=起始日期,@ym=截止日期 from 生产月设置
                where 分厂=2 and 年度='" + this.Year0.SelectedValue + "' and 月份='" + month + "' select isnull(sum(入库重量),0)/1000 from 生产进度 where 分厂ID=2 and 产品类别ID=2 and 日期 between @yc and @ym";
                Cmd.CommandText = sql;
            }
            if (分厂 == "三分厂" && 品名 == "油管接箍")
            {
                string sql = @"
                declare @yc smalldatetime
                declare @ym smalldatetime
                declare @yjiegu float
                declare @tjiegu float
                select @yc=起始日期,@ym=截止日期 from 生产月设置
                where 分厂=3 and 年度='" + this.Year0.SelectedValue + "' and 月份='" + month + "' select @yjiegu=isnull(sum(磷化支数),0) from 生产进度3 where 产品类别ID=1 and 日期 between @yc and @ym select (@yjiegu)";
                
                Cmd.CommandText = sql;
            }
            if (分厂 == "三分厂" && 品名 == "套管接箍")
            {
                string sql = @"
                declare @yc smalldatetime
                declare @ym smalldatetime
                declare @yjiegu float
                declare @tjiegu float
                select @yc=起始日期,@ym=截止日期 from 生产月设置
                where 分厂=3 and 年度='" + this.Year0.SelectedValue + "' and 月份='" + month + "' select @tjiegu=isnull(sum(磷化支数),0) from 生产进度3 where 产品类别ID=2 and 日期 between @yc and @ym select (@tjiegu)";

                Cmd.CommandText = sql;
            }
            if (分厂 == "四分厂" && 品名 == "螺纹保护器")
            {
                string sql = @"
                declare @yc smalldatetime
                declare @ym smalldatetime
                select @yc=起始日期,@ym=截止日期 from 生产月设置
                where 分厂=4 and 年度='" + this.Year0.SelectedValue + "' and 月份='" + month + "' select (isnull(sum(入库),0)) from 生产进度4 where 产品类别ID=2 and 日期 between @yc and @ym";

                Cmd.CommandText = sql;
            }
            if (分厂 == "五分厂" && 品名 == "油管")
            {
                string sql = @"
                declare @yc smalldatetime
                declare @ym smalldatetime
                select @yc=起始日期,@ym=截止日期 from 生产月设置
                 where 分厂=5 and 年度='" + this.Year0.SelectedValue + "' and 月份='" + month + "' select isnull(sum(入库重量),0)/1000 from 生产进度 where 分厂ID=5 and 产品类别ID=1 and 日期 between @yc and @ym";

                Cmd.CommandText = sql;
            }
            SqlDataReader Reader = Cmd.ExecuteReader();
            if (品名 == "螺纹保护器")
            {
                if (Reader.Read())
                {
                    if (Reader.IsDBNull(0))
                    {
                        实际量 = 0;
                    }
                    else
                    {
                        实际量 = Reader.GetInt32(0);
                    }
                }
            }
            else
            {
                if (Reader.Read())
                {
                    if (Reader.IsDBNull(0))
                    {
                        实际量 = 0;
                    }
                    else
                    {
                        实际量 = Reader.GetDouble(0);
                    }
                }
            }
            Reader.Close();
            Conn.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Dispose();
            return 实际量;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            品名 = this.PinMing.SelectedItem.Text;
            if (this.FenChang.SelectedItem.Text == "一分厂" || this.FenChang.SelectedItem.Text == "二分厂" || this.FenChang.SelectedItem.Text == "五分厂")
            {
                钢级 = "J55";
            }
            else
            {
                钢级 = "-";
            }
            if (this.FenChang.SelectedItem.Text == "一分厂" || this.FenChang.SelectedItem.Text == "二分厂" || this.FenChang.SelectedItem.Text == "五分厂")
            {
                单位 = "吨";
            }
            else if (this.FenChang.SelectedItem.Text == "三分厂") 
            {
                单位 = "支";
            } 
            else
            {
                单位 = "套";
            }
            //计划量
            for (int i = 0; i < 12;i++ )
            {
                计划量[i] = 月计划量(i+1, this.FenChang.SelectedItem.Text, this.PinMing.SelectedItem.Text);
            }
            //实际量
            for (int i = 0; i < 12; i++)
            {
                实际量[i] = 月实际量(i + 1, this.FenChang.SelectedItem.Text, this.PinMing.SelectedItem.Text);
            }
            //完成率            
            for (int i = 0; i < 12; i++)
            {
                if (计划量[i] == 0)
                {
                    完成率[i] = 0;
                }
                else
                {
                    完成率[i] = 实际量[i] / 计划量[i];
                }
            }
        }

        protected void PinMing_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.PinMing.SelectedItem.Text == "油管")
            {
                this.SqlDataSource3.SelectCommand = "SELECT [ID], [名称] FROM [生产单位]";
                this.SqlDataSource3.FilterExpression = " 名称='二分厂' or 名称='五分厂'";
                this.FenChang.DataBind();
            }
            if (this.PinMing.SelectedItem.Text == "套管")
            {
                this.SqlDataSource3.SelectCommand = "SELECT [ID], [名称] FROM [生产单位]";
                this.SqlDataSource3.FilterExpression = " 名称='二分厂' or 名称='一分厂'";
                this.FenChang.DataBind();
            } 
            if (this.PinMing.SelectedItem.Text == "油管接箍"||this.PinMing.SelectedItem.Text =="套管接箍")
            {
                this.SqlDataSource3.SelectCommand = "SELECT [ID], [名称] FROM [生产单位]";
                this.SqlDataSource3.FilterExpression = " 名称='三分厂'";
                this.FenChang.DataBind();
            }
            if (this.PinMing.SelectedItem.Text == "螺纹保护器")
            {
                this.SqlDataSource3.SelectCommand = "SELECT [ID], [名称] FROM [生产单位]";
                this.SqlDataSource3.FilterExpression = " 名称='四分厂'";
                this.FenChang.DataBind();
            }
        }       
    }
}
